package com.yupi.yudada.score;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yupi.yudada.manager.AIManager;
import com.yupi.yudada.mapper.QuestionMapper;
import com.yupi.yudada.model.dto.question.QuestionAnswerDTO;
import com.yupi.yudada.model.dto.question.QuestionContentDTO;
import com.yupi.yudada.model.entity.App;
import com.yupi.yudada.model.entity.Question;
import com.yupi.yudada.model.entity.ScoreResult;
import com.yupi.yudada.model.entity.UserAnswer;
import com.yupi.yudada.model.vo.QuestionVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author ortonzhu
 * @Date 2024/11/16 17:09
 */
@Component
public class AiTestScoreStrategy implements ScoreStrategy {
    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private AIManager aiManager;

    private static final String AI_TEST_SCORING_SYSTEM_MESSAGE = "你是一位严谨的判题专家，我会给你如下信息：\n" +
        "```\n" +
        "应用名称，\n" +
        "【【【应用描述】】】，\n" +
        "题目和用户回答的列表：格式为 [{\"title\": \"题目\",\"answer\": \"用户回答\"}]\n" +
        "```\n" +
        "\n" +
        "请你根据上述信息，按照以下步骤来对用户进行评价：\n" +
        "1. 要求：需要给出一个明确的评价结果，包括评价名称（尽量简短）和评价描述（尽量详细，大于 200 字）\n" +
        "2. 严格按照下面的 json 格式输出评价名称和评价描述\n" +
        "```\n" +
        "{\"resultName\": \"评价名称\", \"resultDesc\": \"评价描述\"}\n" +
        "```\n" +
        "3. 返回格式必须为 JSON 对象";
/*
{
  "appName": "MBTI 性格测试",
  "appDesc": "测试你的 MBTI 性格",
  "question": [
    {
      "title": "你喜欢和人交流",
      "answer": "喜欢"
    }
  ]
}
 */
private String getAiTestScoringUserMessage(App app, List<QuestionContentDTO> questionContentDTOList, List<String> choices) {
    StringBuilder userMessage = new StringBuilder();
    userMessage.append(app.getAppName()).append("\n");
    userMessage.append(app.getAppDesc()).append("\n");
    List<QuestionAnswerDTO> questionAnswerDTOList = new ArrayList<>();
    for (int i = 0; i < questionContentDTOList.size(); i++) {
        QuestionAnswerDTO questionAnswerDTO = new QuestionAnswerDTO();
        questionAnswerDTO.setTitle(questionContentDTOList.get(i).getTitle());
        questionAnswerDTO.setUserAnswer(choices.get(i));
        questionAnswerDTOList.add(questionAnswerDTO);
    }
    userMessage.append(JSONUtil.toJsonStr(questionAnswerDTOList));
    return userMessage.toString();
}


    @Override
    public UserAnswer calculateScore(List<String> choice, App app) {
        // 1. 根据id获取题目和 题目结果信息
        Question question = questionMapper.selectOne(Wrappers.<Question>lambdaQuery().eq(Question::getAppId, app.getId()));
        QuestionVO questionVO = QuestionVO.objToVo(question);
        List<QuestionContentDTO> questionContent = questionVO.getQuestionContent();
        // 调用ai
        String userMessage = getAiTestScoringUserMessage(app, questionContent, choice);
        String res = aiManager.doSyncStableRequest(AI_TEST_SCORING_SYSTEM_MESSAGE, userMessage);
        int start = res.indexOf("{");
        int end = res.lastIndexOf("}");
        String json = res.substring(start, end + 1);
        UserAnswer userAnswer = JSONUtil.toBean(json, UserAnswer.class);
        //4. 造返回值，填充答案对象的属性
        userAnswer.setAppId(app.getId());
        userAnswer.setAppType(app.getAppType());
        userAnswer.setScoringStrategy(app.getScoringStrategy());
        userAnswer.setChoices(JSONUtil.toJsonStr(choice));
        return userAnswer;
    }
}
